package com.cskaoyan._19collection._05list.arraylist;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Macro_Ray
 * @since 2024/3/30 15:52
 */
/*
 * 1.main方法中定义一个集合，存入三个用户对象
 * 用户属性为：id，username，password
 * 2，要求：定义一个方法，根据id查找对应的用户信息
 * 如果存在，返回true
 * 如果不存在，返回false*/
public class ArrayListTest5 {
    public static void main(String[] args) {
        ArrayList<User> userList = new ArrayList<>();

        userList.add(new User(1, "zhangsan", "123456"));
        userList.add(new User(2, "lisi", "234567"));
        userList.add(new User(3, "wangwu", "345678"));

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的用户id：");
        int findId = sc.nextInt();

        // 定义一个方法，根据id查找对应的用户信息
        boolean result = SearchById(userList,findId);

        System.out.println(result);
    }

    private static boolean SearchById(ArrayList<User> userList,int findId) {
        for (User u : userList) {
            if (u.getId() == findId) {
                return true;
            }
        }
        return false;
    }
}

class User {
    private int id;
    private String username;
    private String password;

    public User() {
    }

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}